package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.SelectBankAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.selectbankbean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_recycle.*
import kotlinx.android.synthetic.main.title_layout.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.util.*

class SelectBankActivity : BaseActivity(), IView {
    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_recycle
    }

    override fun initData() {
        province = intent.getStringExtra("provinceId")
        city = intent.getStringExtra("cityId")
        area_id = intent.getStringExtra("areaId")
        bank_name = intent.getStringExtra("bank_name")
    }

    override fun initView() {
        tv_title.text = "选择具体开户支行"
        iv_back.onClick { finish() }
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private var province: String = ""

    private var city: String = ""

    private var area_id: String = ""

    private var bank_name: String = ""

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["client"] = "android"
        params["key"] = token
        params["province"] = province
        params["city"] = city
        params["area_id"] = area_id
        params["bank_name"] = bank_name
        datas.clear()
        RetrofitHelper.service.get_bank_subbranch(params).sss(this) {
            it.result.let { it1 -> datas.addAll(it1.bank_subbranch) }
            mAdapter.notifyDataSetChanged()
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<selectbankbean.Bank_subbranch>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: SelectBankAdapter by lazy {
        SelectBankAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
            Intent().run {
                putExtra("bank_name", data.bank_subbranch)
                putExtra("bank_code", data.bank_code)
                putExtra("alliedBankCode", data.allied_bank_code)
                setResult(400, this)
                finish()
            }

        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            this.onItemClickListener = ItemClickListener
        }
    }

}